<?php

namespace app\common\lib;

class Util {

    /**
     * API 输出格式
     * @param $status
     * @param string $message
     * @param array $data
     * @return bool
     */
    public static function show($status, $message = '', $data = []) {
        $result = [
            'status' => $status,
            'message' => $message,
            'data' => $data,
        ];

        echo json_encode($result);
        return true;
    }


// 生成随机字符串
    function randomString( $length=20 ){
        $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
        return substr( str_shuffle( str_repeat($chars, 5) ), 0, $length );
    }

// 生成随机数字串
    function randomNumber( $length=6 ){
        $chars = '0123456789';
        return substr( str_shuffle( str_repeat($chars, 5) ), 0, $length );
    }

    /*
      例子数据
    $list = [
        1=>['id'=>1,'pid'=>0,'name'=>'服装'],
        2=>['id'=>2,'pid'=>0,'name'=>'数码'],
        3=>['id'=>3,'pid'=>0,'name'=>'食品'],
        4=>['id'=>4,'pid'=>1,'name'=>'男装'],
        5=>['id'=>5,'pid'=>1,'name'=>'女装'],
        6=>['id'=>6,'pid'=>1,'name'=>'童装'],
        7=>['id'=>7,'pid'=>1,'name'=>'内衣'],
        8=>['id'=>8,'pid'=>2,'name'=>'平板电脑'],
        9=>['id'=>9,'pid'=>2,'name'=>'摄像摄影'],
        10=>['id'=>10,'pid'=>2,'name'=>'智能数码'],
        11=>['id'=>11,'pid'=>2,'name'=>'娱乐影音'],
        12=>['id'=>12,'pid'=>3,'name'=>'新鲜水果'],
        13=>['id'=>13,'pid'=>3,'name'=>'蔬菜蛋品'],
        14=>['id'=>14,'pid'=>3,'name'=>'精选肉类'],
        15=>['id'=>15,'pid'=>3,'name'=>'海鲜水产'],
        16=>['id'=>16,'pid'=>7,'name'=>'男士内衣'],
        17=>['id'=>17,'pid'=>7,'name'=>'女生内衣'],
    ];
     */

// 二维数组 => 树形结构 id:主键, pid:父级id
    function array2Tree( $rows, $id='id', $pid='pid' ) {
        $items = array();

        foreach ( $rows as $row ) {
            $row[ 'level' ] = 1; // 初始 深度/层数
            $items[ $row[ $id ] ] = $row;
        }
        foreach ($items as $item) {
            $item[ $pid ] != 0 && $items[ $item[ $id ] ][ 'level' ] += $items[ $item[ $pid ] ][ 'level' ]; // 计算 深度/层数
            $items[ $item[ $pid ] ][ 'son' ][ $item[ $id ] ] = &$items[ $item[ $id ] ];
        }

        return isset( $items[ 0 ][ 'son' ] ) ? $items[ 0 ][ 'son' ] : array();
    }

// 树形结构 => 二维数组( 带层级排序 )
    function tree2Array( $tree ) {
        static $arr = array();

        foreach( $tree as $val ) {
            $tem = $val;
            unset( $tem[ 'son' ] );
            $arr[] = $tem;

            if( isset( $val[ 'son' ] ) && !empty( $val['son'] ) ) {
                tree2Array( $val['son'] );
            }
        }
        return $arr;
    }

    function getIp ()
    {
        $ip_address = '';
        if (isset($_SERVER)) {
            if (isset($_SERVER['HTTP_X_REAL_IP'])) {
                $ip_address = $_SERVER["HTTP_X_REAL_IP"];
            } else if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
                $ip_address = $_SERVER["HTTP_X_FORWARDED_FOR"];
            } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
                $ip_address = $_SERVER["HTTP_CLIENT_IP"];
            } else if (isset($_SERVER['REMOTE_ADDR'])) {
                $ip_address = $_SERVER["REMOTE_ADDR"];
            } else {
                // var_dump($ip_address);
            }
        } else {
            if (getenv("HTTP_X_FORWARDED_FOR")) {
                $ip_address = getenv("HTTP_X_FORWARDED_FOR");
            } else if (getenv("HTTP_CLIENT_IP")) {
                $ip_address = getenv("HTTP_CLIENT_IP");
            } else {
                $ip_address = getenv("REMOTE_ADDR");
            }
        }
        return $ip_address;
    }
}